package com.fitsoftwareprocess.mobilemap;

import java.util.ArrayList;

import com.fitsoftwareprocess.mobilemap.bus.Map;
import com.fitsoftwareprocess.mobimap.dto.Coordinate;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.ZoomControls;
import android.graphics.Bitmap;
import android.graphics.Point;

public class HomeActivity extends Activity implements OnTouchListener,OnItemSelectedListener {
	private double longitude = 106.682568;
	private double latitude = 10.762795;
	private int MAX_ZOOM = 16;
	private int MIN_ZOOM = 15;
	private int type = 1;// local
	private int zoom = 15;
	private int width = 350;
	private int height = 480;
	private ImageView _ImageViewMap;
	private Map map;
	private Bitmap bm;
	private float mx1 = 0;
	private float mx2 = 0;
	private float my1 = 0;
	private float my2 = 0;
	private Coordinate _MapCenter;
	private Coordinate _HomeAddress;
	private ArrayList<Coordinate> direction;
	private ZoomControls _ZoomControlMap;
	private Spinner mapType;
	private static final String[] items = { "Local Map","Google Map" };


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		_ImageViewMap = (ImageView) findViewById(R.id.imageViewMap);
		_ZoomControlMap = (ZoomControls) findViewById(R.id.zoomControlsMap);
		mapType =(Spinner)findViewById(R.id.mapType);
		
		ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, items);
		typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mapType.setAdapter(typeAdapter);

		
		CheckZoomControlStatus();
		_ZoomControlMap.setOnZoomInClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(zoom < MAX_ZOOM){
					zoom ++;
	//				ReloadMap();
					CheckZoomControlStatus();
				}
			}
		});
		_ZoomControlMap.setOnZoomOutClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (zoom > MIN_ZOOM) {
					zoom --;
					ReloadMap();
					CheckZoomControlStatus();
				}
			}
		});

		map = new Map("http://tempuri.org/",
				"http://10.0.2.2:1609/Service1.asmx");
		if (getIntent().getExtras() != null) {
			longitude = Double.parseDouble(getIntent().getExtras().getString(
					"long"));
			String lati = getIntent().getExtras().getString("lat");
			latitude = Double.parseDouble(lati);
			direction = (ArrayList<Coordinate>) getIntent().getSerializableExtra("direction");
		}

		if (direction == null) {
			direction = new ArrayList<Coordinate>();
			direction.add(new Coordinate(-1.0, -1.0));
		}
		_MapCenter = new Coordinate(latitude, longitude);
		_HomeAddress = new Coordinate(latitude, longitude);// Green Marker
		ReloadMap();
		_ImageViewMap.setOnTouchListener(this);
		mapType.setOnItemSelectedListener(this);
	}

	private void CheckZoomControlStatus() {
		if(zoom == MAX_ZOOM)
			_ZoomControlMap.setIsZoomInEnabled(false);
		if(zoom > MIN_ZOOM)
			_ZoomControlMap.setIsZoomOutEnabled(true);
		if(zoom == MIN_ZOOM)
			_ZoomControlMap.setIsZoomOutEnabled(false);
		if(zoom < MAX_ZOOM)
			_ZoomControlMap.setIsZoomInEnabled(true);
	}

	private void ReloadMap() {
		//bm = map.getMap(_MapCenter, _HomeAddress, width, height, zoom, type,direction);
		//_ImageViewMap.setImageBitmap(bm);
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.imageViewMap:

			float dx = 0,
			dy = 0;
			float x = event.getX();
			float y = event.getY();
			int eventaction = event.getAction();
			switch (eventaction) {
			case MotionEvent.ACTION_CANCEL:
				break;

			case MotionEvent.ACTION_DOWN:
				mx1 = event.getX();
				my1 = event.getY();
				Log.w(this.getClass().getName(), "In MOTION DOWN" + mx1 + " "
						+ my1);
				break;
			case MotionEvent.ACTION_MOVE:
				mx2 = event.getX();
				my2 = event.getY();
				Log.w(this.getClass().getName(), "In MOTION MOVE" + mx2 + " "
						+ my2);
				break;
			case MotionEvent.ACTION_UP:
				mx2 = event.getX();
				my2 = event.getY();
				dy = (my2 - my1);
				dx = -(mx2 - mx1);
				// _ImageViewMap.scrollBy((int)dx, (int)dy);
				Log.w(this.getClass().getName(), "In MOTION UP " + mx2 + " "
						+ my2);
				// return super.onTouchEvent(event);
				if (dx != 0 || dy != 0) {
					double distance = 0.00008 * Math.pow(2, 20 - zoom);
					if (dx > 0)
						_MapCenter.set_longitude(_MapCenter.get_longitude()
								+ distance);
					else if (dx < 0)
						_MapCenter.set_longitude(_MapCenter.get_longitude()
								- distance);
					if (dy > 0)
						_MapCenter.set_latitude(_MapCenter.get_latitude()
								+ distance);
					else if (dy < 0)
						_MapCenter.set_latitude(_MapCenter.get_latitude()
								- distance);

				ReloadMap();
				}
				break;
			default:
				break;
			}
			Log.d(this.getClass().getName(), "Value of angle in ontouch: "
					+ Float.toString(dy) + " " + Float.toString(dx));
			break;
		default:
			break;
		}
		return true;
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		// TODO Auto-generated method stub
		String temp = items[position];
		if (temp.compareTo("Google Map") == 0) {
			type=0;
			ReloadMap();
		
		}
		else
			if (temp.compareTo("Local Map") == 0) {
				type =1;
				ReloadMap();
			
			}
		

		
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		

		
	}

	
}
